home *** CD-ROM | disk | FTP | other *** search
/ Floppyshop 2 / Floppyshop - 2.zip / Floppyshop - 2.iso / diskmags / 3565-4.665 / dmg-4385 / issue_10 / 5.pne < prev    next >
Text File  |  1987-04-21  |  10KB  |  287 lines

  1.                    MAKING COMPILED PROGRAMS SMALLER
  2.                    --------------------------------
  3.  
  4.  
  5.                            By Martin Cubitt
  6.  
  7.  
  8.                           6th December 1993
  9.  
  10.  
  11.  
  12.     The compiled versions of a STOS program can be mammoth. Even the 
  13. simplest of BASIC programs, when compiled, can produce 50 or 60K of 
  14. compiled code.
  15.  
  16.     A compiled program can, of course, be compressed using one of the 
  17. many packers around. Using the Atomik packer most STOS-compiled 
  18. programs can be reduced to around 50%. Quite a saving when you are 
  19. trying to get as much as possible onto a disc.
  20.  
  21.     Wouldn't it be nice, however, if you were able to reduce the size 
  22. of the compiled code further, before it was compressed? Well the good 
  23. news is that you can but the bad news is that the saving is quite small 
  24. (depending on the size and complexity of your program).
  25.  
  26.     I have mentioned in a previous article that it is not, despite some 
  27. suggestions to the contrary, advantageous to remove the original STOS 
  28. supplied files which occupy the COMPILER folder. You do not use sprites 
  29. in your program? Okay, delete SPRITExxx.BIN and try to compile your 
  30. program. It will compile okay if it does not use ANY sprite function. 
  31. However, bring back the SPRITExxx.BIN and compiler the program again. 
  32. The object size will be the same. If your programs requires any traps 
  33. from the STOS-supplied libraries it will load the entire library and 
  34. use that but if it needs none of them it will not.
  35.  
  36.     So how CAN you make your compiled programs smaller? Well, the 
  37. method I am going to describe will be considered (and quite rightly so) 
  38. very mundane and some will ask "is it worth it?". It is up to the 
  39. individual. If the size of the compiled object MUST be an absolute 
  40. minimum then I suggest you spend time to make it so.
  41.  
  42.     The idea is to remove all 'rem' statements and pack as many 
  43. commands onto one line as possible. Actually, it is the removing of 
  44. unnecessary lines that save the space. Every line, even if it only has 
  45. a rem statement on, takes up 6 bytes. Each one you can delete will save 
  46. you 6 bytes from your compiled program. Wow! Six bytes from 60K? What a 
  47. great saving!
  48.  
  49.     Hold on, the more lines you can delete the more you can save. 
  50. Delete ten unnecessary lines and you have saved 60 bytes, one hundred 
  51. lines and the saving is 600 bytes.
  52.  
  53.     The rem statement is not compiled and nor is the comment after the 
  54. statement. SO the line
  55.  
  56.     10 rem A simple program for a start
  57.  
  58.     will only compile to 6 bytes, because it contains one line. (In 
  59. fact the program will fail to compile because there are no line which 
  60. can be compiled)
  61.  
  62.     A line such as
  63.  
  64.     10 mode 0 : rem Low res screen
  65.  
  66.     will compile to the exactly same size as
  67.  
  68.     10 mode 0
  69.  
  70.     but
  71.  
  72.     10 rem Low res screen
  73.     20 mode 0
  74.  
  75.     will take up 6 more bytes!
  76.  
  77.     I would NEVER suggest when writing your program you do so with the 
  78. compiled version in mind, keep any remarks or comments clear so that 
  79. debugging is made easier for you. Put enough rem statements in the 
  80. program, just prior to compiling THEN strip them out and economise the 
  81. code. (Do not save the de-remmed version otherwise there was no point 
  82. in putting them in initially!).
  83.  
  84.     As all good programmers know, writing clear code requires you to 
  85. keep to a limit of one instructions (except where unavoidable although 
  86. some suggest there is never such an occasion) per line.
  87.  
  88.     That is, rather than
  89.  
  90.     10 SC=0 : LI=3 : ROUND=1 : BONUS=500 : if mode=1 then mode=0
  91.  
  92.     you should use
  93.  
  94.     10 SC=0
  95.     20 LI=3
  96.     30 ROUND=1
  97.     40 BONUS=500
  98.     50 if mode<>1 then 70
  99.     60 mode 0
  100.     70 ...
  101.  
  102.     However, this is not a lesson in structured or clear programming. I 
  103. am going to assume that your programs are fairly structured and well 
  104. REMmed.
  105.  
  106.  
  107.     Once you have finished your program and you have tested it you will 
  108. be ready to compile it. Make sure you save the full BASIC version with 
  109. the structured code and abundant comments.
  110.  
  111.     Now get ready to rip the program to pieces!
  112.  
  113.     To make things a little simpler I have written a small example 
  114. program and show you one method to economise a program.
  115.  
  116.     This is the full program...
  117.  
  118.  10 rem Simple program to show how to
  119.  20 rem make compiled programs slightly
  120.  30 rem smaller.
  121. 100 mode 1
  122. 110 key off 
  123. 120 hide 
  124. 130 curs off 
  125. 140 flash off 
  126. 150 palette $0,$777
  127. 160 curs on 
  128. 170 rem Input person's name
  129. 180 print 
  130. 190 input "Enter your name (or QUIT):";NAME$
  131. 200 print 
  132. 210 if upper$(NAME$)="QUIT" then 900 : rem If QUIT then end
  133. 220 if NAME$<>"" then 260 : rem If not blank then skip error
  134. 230 rem Name is blank, print error and try again
  135. 240 print "You must have a name!"
  136. 250 goto 170
  137. 260 rem Valid name, ask their age
  138. 270 rem Input person's age
  139. 280 print 
  140. 290 input "Now tell me your age (-1 to quit):";AGE# : rem Allow decimal, eg for 11.5 for eleven and a half
  141. 300 print 
  142. 310 if AGE#=-1 then 900
  143. 320 if AGE#>0 then 360 : rem If greater than 0 then skip error
  144. 330 rem Age is 0 or below, print error and try again
  145. 340 print "You must be born to use this!"
  146. 350 goto 280
  147. 360 if AGE#<100 then 500 : rem If age less than 100 ignore anymore fancy validation
  148. 370 rem Age is at least 100. Check a few ranges and give appropriate messages.
  149. 380 if AGE#>120 then 420 : rem Age greater than 120, unlikely but poss
  150. 390 rem Age between 100 and 120 (good ages but possible)
  151. 400 print "Congratulations, that is quite an age!"
  152. 410 goto 500
  153. 420 rem Age is more than 120, check once more
  154. 430 if AGE#<150 then 470 : rem Age less than 150, maybe possible!
  155. 440 rem Age greater than or equal to 150, I don't think so!
  156. 450 print "What? You ARE joking. Be serious this time!"
  157. 460 goto 270
  158. 470 rem Age high but remotely possible
  159. 480 print "Many congratulations, that is an exceptional age!"
  160. 490 goto 500
  161. 500 WEEKS#=AGE#*52
  162. 510 clw 
  163. 520 print 
  164. 530 print "Now then ";NAME$;","
  165. 540 print "You tell me that you are";AGE#;" years"
  166. 550 print "old. Let me see how many weeks that is..."
  167. 560 wait 120
  168. 570 print 
  169. 580 print "Well, I make it";WEEKS#;" weeks!"
  170. 590 print 
  171. 600 print 
  172. 610 print 
  173. 620 print 
  174. 630 print 
  175. 640 print 
  176. 650 print 
  177. 660 print 
  178. 670 print 
  179. 680 print 
  180. 690 print 
  181. 700 print 
  182. 710 print 
  183. 720 print 
  184. 730 print 
  185. 740 print "  NEXT PERSON PLEASE..."
  186. 750 print 
  187. 760 goto 170
  188. 900 default
  189. 910 end 
  190.  
  191.  
  192.     Okay, so it is not as small as I thought! Anyhow, let us start to 
  193. economise...
  194.  
  195.     I happen to know that lines 10 - 30 are comment lines. I do not 
  196. even need to check if there is a goto 10,20 or 30 because I know there 
  197. is not. Therefore these lines can be deleted immediately saving exactly 
  198. 6 x 3 = 18 bytes!
  199.  
  200.     I also know that lines 100 - 160 can be merged so I enter
  201.  
  202. 100 mode 1 : key off : hide : curs off : flash off : palette $0,$777 :
  203.     curs on 
  204.  
  205.     after deleting 110 - 160 I have saved a further 6 x 6 = 36 bytes. 
  206. That is 54 bytes so far!
  207.  
  208.     Line 170 is a simple rem line but I cannot just delete it. I have 
  209. to check to see if any other line references it.
  210.  
  211.     Firstly I delete the line
  212.            170
  213.     I then enter
  214.            search "170
  215.     the computer returns
  216.            250 goto 170
  217.  
  218.     Ah ha! I check the program and find the first 'real' line is at 180 
  219. so I change line 250 to
  220.            250 goto 180
  221.  
  222.     I check any more...
  223.            search
  224.     the computer returns
  225.            760 goto 170
  226.  
  227.     I change this to
  228.            760 goto 180
  229.  
  230.     more?
  231.            search
  232.     the computer returns
  233.            search failed
  234.  
  235.  
  236.     Basically I go through the entire program (quite a chore, even with 
  237. a small program) economising. Try not to change the actual logic (what 
  238. it does) unless it is a clear improvement. For example, with cunning 
  239. use of the else command you can save many more lines. Any errors in 
  240. your program (such as spelling) that you identify as you go through jot 
  241. down so that you can apply the changes to the "real" source afterwards.
  242.  
  243.     After economising the above program I came up with...
  244.  
  245. 100 mode 1 : key off : hide : curs off : flash off : palette $0,$777 :
  246.  curs on 
  247. 180 print : input "Enter your name (or QUIT):";NAME$ : print :
  248.  if upper$(NAME$)="QUIT" then end else if NAME$="" then
  249.  print "You must have a name!" : goto 180
  250. 280 print : input "Now tell me your age (-1 to quit):";AGE# : print : 
  251.  if AGE#=-1 then end else if AGE#<=0 then 
  252.  print "You must be born to use this!" : goto 280
  253. 360 if AGE#<100 then 500 else if AGE#<=120 then 
  254.  print "Congratulations, that is quite an age!" : goto 500
  255. 430 if AGE#>=150 then
  256.  print "What? You ARE joking. Be serious this time!" : goto 280
  257.  else print "Many congratulations, that is an exceptional age!"
  258. 500 WEEKS#=AGE#*52 : clw : print : print "Now then ";NAME$;"," : print
  259.  "You tell me that you are";AGE#;" years" : print "old. Let me see how
  260.  many weeks that is..." : wait 120 : print : print "Well, I make it";
  261.  WEEKS#;" weeks!" : locate 0,21 : print "  NEXT PERSON PLEASE..." :
  262.  print : goto 180
  263.  
  264.  
  265.     Notice that I have deleted lines 900 and 910. I replaced the goto 
  266. 900 with straight 'end's and delete the 'default' completely. There is 
  267. not need for default in a compiled program.
  268.  
  269.     All those prints have been replaced with one locate 0,21 a much 
  270. better command in this instance.
  271.  
  272.  
  273.     So how much space have I saved?
  274.  
  275.     Well, I compiled the original program as a .PRG and it totalled 
  276. 66,512 bytes. The economised program compiled to 65,797 bytes 
  277. representing a saving of 716 bytes.
  278.  
  279.     Was it worth it? If I had originally been 716 bytes short when 
  280. trying to save it to disc then yes! Seriously though you may consider 
  281. economising your programs but be careful when changing a line of 
  282. commands to another that you do not omit anything or incorrectly spell 
  283. text from the original.
  284.  
  285.  
  286.     Happy economising!
  287.